package com.wuage.infrastructure.handler;

import com.wuage.infrastructure.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Shynin
 * @version 1.0
 * @date 2020-09-04 18:40
 */
@Slf4j
@Component
public class JwtTokenFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {

        String tokenHeader = request.getHeader(JwtUtils.TOKEN_HEADER);
        if (tokenHeader == null) {
            //当token不存在，直接放行给下面的filter去处理
            chain.doFilter(request, response);
        } else {
            logger.info("------------------------------------------");
            // 当token存在时，判断token (未完成全部功能)
            if(!tokenHeader.startsWith(JwtUtils.TOKEN_PREFIX)) {
                log.info("token is uncorrected");
            }
            chain.doFilter(request, response);
        }
    }
}
